<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>群组管理</el-breadcrumb-item>
      <el-breadcrumb-item>群组管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表主体部分 -->
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索区域 -->
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGroupList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGroupList"
            ></el-button>
          </el-input>
        </el-col>
        <!-- 搜索按钮 -->
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加群组</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表 border边框 stripe隔行变色-->
      <el-table :data="groupList" border="" stripe="">
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="群组名" prop="groupName"></el-table-column>
        <el-table-column label="所属租户" prop="tenantName"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="ViewMembers(scope.row.gid, scope.row.groupName)"
            ></el-button>
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.gid, scope.row.groupName)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGroup(scope.row.gid)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 新增用户对话框 -->
    <el-dialog
      title="添加群组"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="群组名" prop="name">
          <el-input
            v-model="addForm.groupname"
            placeholder="请输入群组名"
          ></el-input>
        </el-form-item>
      </el-form>

      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addGroup">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog
      title="修改群组"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="群组名" prop="name">
          <el-input v-model="editForm.groupname"></el-input>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="editUserInfo">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
import Vue from "vue";
export default {
  created() {
    this.username = window.sessionStorage.getItem("name");
    this.getGroupList();
  },

  data() {
    return {
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        pageNum: 1, //当前页
        pageSize: 5, //每一页最大数
      },
      groupList: [], //群组列表
      total: 0, //总记录数
      addDialogVisible: false, //对话框状态
      username: "",
      tenantname: "",
      //添加表单信息
      addForm: {
        groupname: "",
        username: "",
        tenantname: "",
      },

      //修改群组信息
      editForm: {
        gid: "",
        groupname: "",
      },

      //显示或者隐藏修改用户栏
      editDialogVisible: false,

      //添加表单验证
      addFormRules: {
        groupname: [
          { required: true, message: "请输入群组名", trigger: "blur" },
          {
            min: 1,
            max: 12,
            message: "长度在 1 到 12 个字符",
            trigger: "blur",
          },
        ],
      },

      //修改表单验证
      editFormRules: {
        groupname: [
          { required: true, message: "请输入群组名", trigger: "blur" },
          {
            min: 1,
            max: 12,
            message: "长度在 1 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //获取所有群组
    async getGroupList() {
      await this.$http({
        method: "post",
        url: "/getGroupListY",
        data: {
          query: this.queryInfo.query,
          pageNum: this.queryInfo.pageNum,
          pageSize: this.queryInfo.pageSize,
          name: this.username,
        },
      }).then((response) => {
        this.groupList = response.data.data.list;
        this.total = response.data.data.total;
        this.tenantname = response.data.data.qid;
        // console.log("tenantname成功了吗"+this.tenantname)
      });
    },

    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getGroupList();
    },

    //pageNum触发的动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getGroupList();
    },

    //监听添加用户
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    // 增加用户
    addGroup() {
      this.addForm.username = this.username;
      this.addForm.tenantname = this.tenantname;

      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        //添加群组>>>>>>>>>>>>>>???????????????????表单提交验证
        await this.$http({
          method: "post",
          url: "/createGroup",
          data: {
            groupName: this.addForm.groupname,
            tenantName: this.addForm.tenantname,
            userName: this.addForm.username,
          },
        }).then((res) => {
          const response = res.data;
          if (response.code == "666") {
            this.addForm.groupname = "";
            this.$message.success(response.message);
            this.addDialogVisible = false;
            this.getGroupList();
          } else {
            this.$message.error(response.message);
          }
        });
      });
    },

    //根据主键删除用户
    async deleteGroup(gid) {
      const confirmResult = await this.$confirm(
        "此操作将永远删除群组，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        //取消
        return this.$message.info("已取消删除");
      }
      await this.$http({
        method: "post",
        url: "/deleteGroup?gId=" + gid,
      }).then((res) => {
        if (res.data.code == "666") {
          this.$message.success(res.data.message);
          this.getGroupList();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },

    //查看群组成员
    ViewMembers(gid, groupname) {
      window.sessionStorage.setItem("tenantname", this.tenantname);
      window.sessionStorage.setItem("gid", gid);
      window.sessionStorage.setItem("groupname", groupname);

      this.$router.push({
        path: "/groupmembers",
        query: {
          name: "用户管理群组",
        },
      }); //页面路由跳转
    },

    //显示对话框
    async showEditDialog(gid, groupname) {
      this.editForm.groupname = groupname;
      this.editForm.gid = gid;
      this.editDialogVisible = true;
    },

    //关闭窗口
    editDialogClosed() {
      this.$refs.editFormRef.resetFields(); //重置信息
    },

    //确认修改
    editUserInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        await this.$http({
          method: "post",
          url: "/modifyGroup",
          data: {
            gid: this.editForm.gid,
            groupName: this.editForm.groupname,
            tenantName: this.addForm.tenantname,
            userName: this.addForm.username,
          },
        }).then((res) => {
          if (res.data.code == "666") {
            this.$message.success(res.data.message);
            //隐藏
            this.editDialogVisible = false;
            this.getGroupList();
          } else {
            this.$message.error(res.data.message);
          }
        });
      });
    },
  },
};
</script>
  
<style lang="less" scoped>
/* 面包屑样式 */
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>
